function vox = msk_Coords(hfile, csc)
% MSK::Coords  - return a list of coordinates in a mask
%
% FORMAT:       vox = msk.Coords([convention])
%
% Input fields:
%
%       convention  either 'bvint', {'bvsys'}, or 'tal'
%
% Output fields:
%
%       vox         Vx3 list of coordinates

% Version:  v0.7b
% Build:    7090213
% Date:     Sep-02 2007, 1:02 PM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 1 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'msk')
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid call to ''%s''.', ...
        mfilename ...
    );
end
bc = bvqxfile_getcont(hfile.L);
if nargin < 2 || ...
   ~ischar(csc) || ...
   ~any(strcmpi(csc(:)', {'bvint', 'bvsys', 'int', 'sys', 'tal'}))
    csc = 'bvsys';
else
    csc = lower(csc(:)');
end

% find coordinates
[c1, c2, c3] = ind2sub(size(bc.Mask), find(bc.Mask ~= 0));

% multiply correctly and add ?Start Value
res = bc.Resolution;
c1 = (c1 - 1) .* res + bc.XStart;
c2 = (c2 - 1) .* res + bc.YStart;
c3 = (c3 - 1) .* res + bc.ZStart;

% what convention
switch (csc)
    
    % BV internal
    case {'bvint', 'int'}
        vox = [c1(:), c2(:), c3(:)];
    
    % BV internal
    case {'bvsys', 'sys'}
        vox = [c3(:), c1(:), c2(:)];
    
    % BV internal
    case {'tal'}
        vox = 128 - [c3(:), c1(:), c2(:)];
end
